package pers.vinsonws.jianghu.serverspring.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.vinsonws.jianghu.serverspring.entity.ChatRooms;
import pers.vinsonws.jianghu.serverspring.entity.Users;
import pers.vinsonws.jianghu.serverspring.repositories.ChatRoomsRepository;
import pers.vinsonws.jianghu.serverspring.repositories.UsersRepository;
import pers.vinsonws.jianghu.serverspring.tools.Transition;
import pers.vinsonws.jianghu.util.ChatRoom;
import pers.vinsonws.jianghu.util.User;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @auther vinsonws
 * @date 2018/11/5 23:18
 */
@Service
public class ChatRoomServiceImpl implements ChatRoomService {
    private static final Logger log = LoggerFactory.getLogger(ChatRoomServiceImpl.class);
    @Autowired
    private UsersRepository usersRepository;

    @Autowired
    private ChatRoomsRepository chatRoomsRepository;

    @Override
    public List<ChatRoom> getAllChatRoom() {
        List<ChatRooms> chatRoomsList = chatRoomsRepository.findAll();
        List<ChatRoom> chatRoomList = new ArrayList<>();
        for (ChatRooms chatRooms:chatRoomsList){
            chatRoomList.add(Transition.fromChatRoomsToChatRoom(chatRooms,
                    new ChatRoom(),
                    Transition.fromUsersToUser(usersRepository.findById(chatRooms.getMasterId()), new User())));
        }
        return chatRoomList;
    }

    @Override
    public Set<ChatRoom> getJoinedChatRoom(int userId) {

        Set<ChatRooms> chatRoomsSet = usersRepository.findById(userId).getChatRooms();
        Set<ChatRoom> chatRoomSet = new HashSet<>();
        for (ChatRooms chatRooms:chatRoomsSet){
            chatRoomSet.add(Transition.fromChatRoomsToChatRoom(chatRooms,
                    new ChatRoom(),
                    Transition.fromUsersToUser(usersRepository.findById(chatRooms.getMasterId()), new User())));
        }
        return chatRoomSet;
    }

    @Transactional
    @Override
    public Set<User> getAllUserByChatRoomId(int chatRoomId) {
        ChatRooms chatRooms = chatRoomsRepository.findById(chatRoomId);
        Set<User> userSet = new HashSet<>();
//        chatRooms.getUsers().size();
        Set<Users> usersSet = chatRooms.getUsers();
        for (Users users:usersSet){
            userSet.add(Transition.fromUsersToUser(users, new User()));
        }
        return userSet;
    }

    @Override
    public ChatRoom getChatRoomById(int chatRoomId) {
        ChatRooms chatRooms = chatRoomsRepository.findById(chatRoomId);
        return Transition.fromChatRoomsToChatRoom(chatRooms,
                new ChatRoom(),
                Transition.fromUsersToUser(usersRepository.findById(chatRooms.getMasterId()), new User()));
    }

    @Override
    public ChatRooms getChatRoomsById(int chatRoomId) {
        return chatRoomsRepository.findById(chatRoomId);
    }


}
